/**
 * 
 */
package com.kth.subtranslator.driver;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.kth.subtranslator.IBatchTranslateCallBack;
import com.kth.subtranslator.ITranslateDriver;
import com.kth.subtranslator.ITranslateDriverConfiguration;
import com.kth.subtranslator.TranslateEngine;
import com.kth.subtranslator.TranslateEngine.Language;
import com.memetix.mst.translate.Translate;

/**
 * @author axddd
 *
 */
public class BingTranslateDriver implements ITranslateDriver {

	private final Map<Language, com.memetix.mst.language.Language> languageMapping = new HashMap<TranslateEngine.Language, com.memetix.mst.language.Language>();

	public BingTranslateDriver() {
		languageMapping.put(Language.ENGLISH, com.memetix.mst.language.Language.ENGLISH);
		languageMapping.put(Language.FRENCH, com.memetix.mst.language.Language.FRENCH);
	}
	
	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#translate(java.lang.String, com.kth.subtranslator.TranslateEngine.Language, com.kth.subtranslator.TranslateEngine.Language)
	 */
	@Override
	public String translate(String text, Language fromLang, Language toLang) {
		try {
			Translate.setKey("340T8Qvhpvxyq8yDkjrQloEHe/AYg9hQT8XOhjyt0e8=");
			String translatedContent = Translate.execute(text, languageToMetrixLanguage(fromLang), languageToMetrixLanguage(toLang));
			return translatedContent;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#translate(java.util.Collection, com.kth.subtranslator.TranslateEngine.Language, com.kth.subtranslator.TranslateEngine.Language)
	 */
	@Override
	public Collection<String> batchTranslate(Collection<String> text,
			Language fromLang, Language toLang, IBatchTranslateCallBack callBack) {
		try {
			Translate.setKey("340T8Qvhpvxyq8yDkjrQloEHe/AYg9hQT8XOhjyt0e8=");
			String[] translatedContent = Translate.execute(text.toArray(new String[text.size()]), languageToMetrixLanguage(fromLang), languageToMetrixLanguage(toLang));
			return Arrays.asList( translatedContent );
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#getSupportedLanguage()
	 */
	@Override
	public Collection<Language> getSupportedLanguage() {
		return languageMapping.keySet();
	}
		
	private com.memetix.mst.language.Language languageToMetrixLanguage(Language lang) {
		return languageMapping.get(lang);
	}

	@Override
	public String getName() {
		return "Bing translate driver";
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#stop()
	 */
	@Override
	public void stop() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#getSupportedTargetLanguage(com.kth.subtranslator.TranslateEngine.Language)
	 */
	@Override
	public Collection<Language> getSupportedTargetLanguage(Language fromLang) {
		return getSupportedLanguage();
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#getConfiguration()
	 */
	@Override
	public ITranslateDriverConfiguration getConfiguration() {
		// TODO Auto-generated method stub
		return null;
	}

}
